/*
 * FileAccessException.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.access;

import java.text.MessageFormat;

import de.studiorutton.gerengdic.LearningWordsApplication;
import de.studiorutton.gerengdic.R;

import android.content.Context;

public class FileAccessException extends Exception {
	private static final long serialVersionUID = -5612183110177352064L;

	private String paramOne;
	private String paramTwo;
	private ErrorCode error;

	public enum ErrorCode {
		ERROR_UNKNOWN, ERROR_CANNOT_CREATE_DIR, ERROR_CANNOT_CREATE_FILE, 
		ERROR_WEIRD_WRITING, ERROR_CANNOT_READ, ERROR_NO_CHARSET_FOR_READER, 
		ERROR_CANNOT_FIND_EXTERNAL_STORAGE, ERROR_CANNOT_READ_EXTERNAL_STORAGE, 
		ERROR_CANNOT_WRITE_EXTERNAL_STORAGE
	};

	public FileAccessException(String msg) {
		super(msg);
	}

	public FileAccessException(Exception e) {
		super(e.getLocalizedMessage());
	}

	public FileAccessException(ErrorCode error) {
		this.error = error;
	}

	public FileAccessException(ErrorCode error, String param) {
		setErrorCodeAndParam(error, param);
	}

	public FileAccessException(ErrorCode error, String paramOne, String paramTwo) {
		setErrorCodeAndParams(error, paramOne, paramTwo);
	}

	public void setErrorCodeAndParam(ErrorCode error, String param) {
		this.paramOne = param;
		this.error = error;
	}

	public void setErrorCodeAndParams(ErrorCode error, String paramOne,
			String paramTwo) {
		this.error = error;
		this.paramOne = paramOne;
		this.paramTwo = paramTwo;
	}

	public ErrorCode getError() {
		return error;
	}

	@Override
	public String getLocalizedMessage() {
		Context c = LearningWordsApplication.get();

		int id = R.string.error_unknown;
		switch (error) {
		case ERROR_CANNOT_CREATE_DIR:
			id = R.string.error_directory_cannot_be_created;
			break;
		case ERROR_CANNOT_CREATE_FILE:
			id = R.string.error_file_cannot_be_created;
			break;
		case ERROR_WEIRD_WRITING:
			id = R.string.error_weird_writing;
			break;
		case ERROR_CANNOT_READ:
			id = R.string.error_cannot_read_file;
			break;
		case ERROR_NO_CHARSET_FOR_READER:
			id = R.string.error_no_charset_for_reader;
			break;
		case ERROR_CANNOT_FIND_EXTERNAL_STORAGE:
			id = R.string.error_cannot_find_external_storage;
			break;
		case ERROR_CANNOT_READ_EXTERNAL_STORAGE:
			id = R.string.error_cannot_read_external_storage;
			break;
		case ERROR_CANNOT_WRITE_EXTERNAL_STORAGE:
			id = R.string.error_cannot_write_external_storage;
			break;
		default:
			id = R.string.error_unknown;
		}

		String msg = c.getResources().getString(id);
		String result = msg;
		if (paramTwo != null && paramOne != null) {
			Object[] args = { paramOne, paramTwo };
			result = MessageFormat.format(msg, args);
		} else if (paramOne != null) {
			Object[] args = { paramOne };
			result = MessageFormat.format(msg, args);
		}

		return result;
	}

}
